package com.presto.directo.mvvm.viewmodel

import android.app.Application
import android.content.Context
import android.graphics.Bitmap
import androidx.lifecycle.MutableLiveData
import com.forjrking.lubankt.Luban
import com.presto.directo.entity.PersonInfoEntity
import com.presto.directo.entity.Values
import com.presto.directo.net.getHttpMap
import com.presto.directo.net.sign
import com.presto.directo.utils.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class InfoViewModel(application: Application) : BaseViewModel(application) {

    var frontPath: String? = null
    var reversePath: String? = null
    var livingPath: String? = null
    var frontId: String = ""
    var reverseId: String = ""
    var livingId: String = ""

    val imgLiveData = MutableLiveData<PhotoType>()
    val infoLiveData = MutableLiveData<PersonInfoEntity>()
    val uploadLiveData = MutableLiveData<Any>()
    val ocrLiveData = MutableLiveData<Values>()

    fun getInfo() {
        start(onNext = {
            showProgress()
            getApiService().getPersonInfo(getHttpMap().sign(22)).getResultOrNull()?.let {
                frontId = it.frontId
                reverseId = it.reverseId
                livingId = it.livingId
                infoLiveData.value = it
            }
        }, onComplete = {
            dismissProgress()
        })
    }

    fun compressor(context: Context, type: PhotoType, ocr: Boolean = false) {
        val path = if (type == PhotoType.FRONT) frontPath else if (type == PhotoType.REVERSE) reversePath else livingPath
        if (path.isNullOrBlank()) return

        Luban.with()
            .load(path)
            .setOutPutDir(context.externalCacheDir?.path)
            .rename { System.currentTimeMillis().toString() + ".jpg" }
            .format(Bitmap.CompressFormat.JPEG)
            .quality(90)
            .ignoreBy(500)
            .compressObserver {
                onSuccess = {
                    upload(it, type, ocr)
                }
            }
            .launch()
    }

    fun upload(file: File, type: PhotoType, ocr: Boolean = false) {
        start(onNext = {
            showProgress()
            val id = ImageUtil.upload(file.path)
            if (type == PhotoType.FRONT) {
                frontId = id
            } else if (type == PhotoType.REVERSE) {
                reverseId = id
            } else {
                livingId = id
            }
            imgLiveData.value = type
            if (type == PhotoType.FRONT && ocr) {
                val ocrEntity = getThirdApiService().getOcrAdvance(MultipartBody.Part.createFormData("image", file.name, file.asRequestBody("image/*".toMediaType())))
                if (ocrEntity.code == "SUCCESS") {
                    ocrEntity.data?.values?.let {
                        ocrLiveData.value = it
                    }
                }
            }
        }, onComplete = {
            dismissProgress()
        })
    }

    suspend fun uploadInfo(map: MutableMap<String, Any?>) {
        getApiService().savePersonInfo(map.sign(23)).getResultOrNull()
        uploadLiveData.value = true
    }

}

enum class PhotoType {
    FRONT,
    REVERSE,
    LIVE
}